package org.zn.mvcbase.rest;

import java.io.IOException;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class EntityBody{
	
 	private JsonNode bodyNode;
 	private ObjectMapper mapper;
 	
 	private HttpHeaders headers; 
	 
 	private  HttpStatus status;

 	public EntityBody() {
		// TODO Auto-generated constructor stub
	}
 	
 	public EntityBody(JsonNode bodyNode,ObjectMapper mapper) {
		this.bodyNode=bodyNode;
		this.mapper=mapper;
 		
	}
 
 	public EntityBody(JsonNode bodyNode, ObjectMapper mapper, HttpHeaders headers, HttpStatus status) {
		super();
		this.bodyNode = bodyNode;
		this.mapper = mapper;
		this.headers = headers;
		this.status = status;
	}

	public JsonNode getBodyNode() {
		return bodyNode;
	}

	public void setBodyNode(JsonNode bodyNode) {
		this.bodyNode = bodyNode;
	}

	public ObjectMapper getMapper() {
		return mapper;
	}

	public void setMapper(ObjectMapper mapper) {
		this.mapper = mapper;
	}

	public HttpHeaders getHeaders() {
		return headers;
	}

	public void setHeaders(HttpHeaders headers) {
		this.headers = headers;
	}

	public HttpStatus getStatus() {
		return status;
	}

	public void setStatus(HttpStatus status) {
		this.status = status;
	}

	public EntityBody find(String nodeName) {
 		
 		EntityBody  findNode=new EntityBody(bodyNode.findPath(nodeName), mapper,headers,status);
		return findNode;
 		
 	}
 	
 	public EntityBody finds(String nodeName) throws JsonNodeFindException {
 		
 	     String[] el= nodeName.split(".");
 	 
 	     JsonNode node=bodyNode;
 	     
 	     for (String n : el) {
			
 	    	 if(node!=null) {
 	    	node=node.findPath(n);
 	    	 }else {
 	    		 
 	    		 throw new JsonNodeFindException("JsonNode find faild");
 	    		 
 	    	 } 	    	 
		}
 	
 		EntityBody  findNode=new EntityBody(node, mapper,headers,status);
	
 		return findNode;
		
 	}
 	
 	
 	public EntityBody findParent(String nodeName) {
 		
 		EntityBody  findNode=new EntityBody(bodyNode.findParent(nodeName), mapper,headers,status);
		return findNode;
 		
 	}
 	
 
 	 public <T> T toJavaObject(Class<T> type) throws JsonParseException, JsonMappingException, IOException{
 		 
		return mapper.readValue(bodyNode.binaryValue(), type);
 	}
 	 
 	public <T> T toJavaObject(JavaType type) throws JsonParseException, JsonMappingException, IOException{
		 
		return mapper.readValue(bodyNode.binaryValue(), type);
 	}
 	
 	public <T> T toJavaObject(TypeReference<T> type) throws JsonParseException, JsonMappingException, IOException{
		 

		return mapper.readValue(bodyNode.toString(), type);
 	}
 	

}
